//
//  SocketManager.h
//  Common
//
//  Created by shiqing guo on 2018/9/27.
//  Copyright © 2018年 www.bitkinetic.com. All rights reserved.
//

#import <Foundation/Foundation.h>

#define SM [SocketManager shareManager]

/**
 *  socket状态
 */
typedef NS_ENUM(NSInteger,SocketStatus){
    SocketStatusConnected,      // 已连接
    SocketStatusFailed,         // 失败
    SocketStatusClosedByServer, // 系统关闭
    SocketStatusClosedByUser,   // 用户关闭
    SocketStatusReceived         // 接收消息
};

/**
 *  消息类型
 */
typedef NS_ENUM(NSInteger,SocketReceiveType){
    SocketReceiveTypeForMessage, // 正常消息
    SocketReceiveTypeForPong     // 心跳消息
};


// 连接成功回调
typedef void(^SocketDidConnectBlock)(void);
// 失败回调
typedef void(^SocketDidFailBlock)(NSError *error);
// 关闭回调
typedef void(^SocketDidCloseBlock)(NSInteger code, NSString *reason, BOOL wasClean);
// 消息接收回调
typedef void(^SocketDidReceiveBlock)(id message, SocketReceiveType type);


NS_ASSUME_NONNULL_BEGIN

@interface SocketManager : NSObject

/**
 *  连接回调
 */
@property (nonatomic, copy) SocketDidConnectBlock connect;
/**
 *  接收消息回调
 */
@property (nonatomic, copy) SocketDidReceiveBlock receive;
/**
 *  失败回调
 */
@property (nonatomic, copy) SocketDidFailBlock failure;
/**
 *  关闭回调
 */
@property (nonatomic, copy) SocketDidCloseBlock close;
/**
 *  当前的socket状态
 */
@property (nonatomic, assign, readonly) SocketStatus socketStatus;

/**
 *  重连2^n次 默认为64秒
 */
@property (nonatomic, assign) NSUInteger reconnectAllTime;

/**
 *  单例调用
 */
+ (instancetype)shareManager;

/**
 *  开启socket
 *
 *  @param urlStr  服务器地址
 *  @param connect 连接成功回调
 *  @param receive 接收消息回调
 *  @param failure 失败回调
 */
- (void)open:(NSString *)urlStr connect:(SocketDidConnectBlock)connect receive:(SocketDidReceiveBlock)receive failure:(SocketDidFailBlock)failure;

/**
 *  关闭socket
 *  @param close 关闭回调
 */
- (void)close:(SocketDidCloseBlock)close;

/**
 *  发送消息，NSString 或者 NSData
 *  @param data Send a UTF8 String or Data.
 */
- (void)send:(id)data;


/**
 *  初始化心跳，启动心跳定时器
 */
- (void)initHeartBeat;
// 取消心跳
- (void)destoryHeartBeat;

- (void)reConnect;

@end

NS_ASSUME_NONNULL_END
